import Vue from "vue";
import Router from "vue-router";
import login from "./components/login.vue";
import home from "./components/home.vue";
import welcome from "./components/welcome.vue";
import User from "./components/User.vue";
import Rights from "./components/rights.vue";
import Roles from "./components/Roles.vue";
import Card from "./components/Card.vue";
import Params from "./components/Params.vue";
import Goods from "./components/Goods.vue";
import addGoods from "./components/addGoods.vue";
import Order from "./components/Order.vue";
import Reports from "./components/reports.vue"
Vue.use(Router);

const router = new Router({
  routes: [
    {
      path: "/",
      redirect: "/login",
    },
    {
      path: "/login",
      component: login,
    },
    {
      path: "/home",
      component: home,
      children: [
        {
          path: "/welcome",
          component: welcome,
        },
        {
          path: "/",
          redirect: "/welcome",
        },
        {
          path: "/users",
          component: User,
        },
        {
          path: "/rights",
          component: Rights,
        },
        {
          path: "/roles",
          component: Roles,
        },
        {
          path: "/categories",
          component: Card,
        },
        {
          path: "/params",
          component: Params,
        },
        {
          path: "/goods",
          component: Goods,
        },
        {
          path: "/goods/add",
          component: addGoods,
        },
        {
          path: "/orders",
          component: Order,
        },
        {
          path:'/reports',
          component:Reports
        }
      ],
    },
  ],
});

router.beforeEach((to, from, next) => {
  if (to.path == "/login") {
    return next();
  } else {
    var token = sessionStorage.getItem("token");
    if (token) {
      return next();
    } else {
      next("/login");
    }
  }
});

export default router;
